<?php

class ProjectDAO {
    public static function getProjectData($pid) {
        $db = Database::getInstance();
        $db->query("SELECT * FROM projects WHERE pid=$pid");
        if($db->getNumRows() > 0) {
            return $db->dbToObjectKeys($db->getRowAssoc());
        } else {
            return array();
        }
    }
    
    public static function create($project) {
        $db = Database::getInstance();
        $db->query("INSERT INTO projects(name,description,date_start,date_end,priority) VALUES(
            '" . $project->name . "',
            '" . $project->description . "',
            '" . $project->dateStart . "',
            '" . $project->dateEnd . "',
            " . $project->priority . "
        )");
        
        if($db->getAffectedRows() > 0) {
            return $db->insertId();
        } else {
            return 0;
        }
    }
    
    public static function update($project) {
        $db = Database::getInstance();
        $db->query("UPDATE
                        projects
                    SET
                        name = '" . $project->name . "',
                        description = '" . $project->description . "',
                        date_start = '" . $project->dateStart . "',
                        date_end = '" . $project->dateEnd . "',
                        priority = " . $project->priority . "
                    WHERE
                        pid = " . $project->getPid());
        return $db->getAffectedRows() > 0;
    }
    
    public static function getTasks($project) {
        $db = Database::getInstance();
        $db->query("SELECT ptid FROM project_tasks WHERE pid=" . $project->getPid());
        if($db->getNumRows() > 0) {
            $tasks = array();
            foreach($db->getField('ptid') as $key => $ptid) {
                $tasks[] = new ProjectTask((int)$ptid);
            }
            return $tasks;
        } else {
            return array();
        }
    }
    
    public static function getSignedTasks($user, $activeOnly=false) {
        $db = Database::getInstance();
        $db->query("SELECT pttu.ptid FROM project_tasks_to_users pttu LEFT JOIN project_tasks pt ON(pttu.ptid = pt.ptid) WHERE pttu.uid=" . $user->getUid() . ($activeOnly ? ' AND pt.done=0' : ''));
        if($db->getNumRows() > 0) {
            $tasks = array();
            foreach($db->getField('ptid') as $key => $ptid) {
                $tasks[] = new ProjectTask((int)$ptid);
            }
            return $tasks;
        } else {
            return array();
        }
    }
    
    public static function exists($project) {
        if(is_int($project)) {
            $project = new Project($project);
        }
        $db = Database::getInstance();
        if($project->getPid() > 0) {
            $db->query("SELECT * FROM projects WHERE pid=" . $project->getPid());
            if($db->getNumRows() > 0) {
                return true;
            } else {
                return false;
            }
        } else {
            $db->query("SELECT * FROM projects WHERE name LIKE '" . $project->name . "'");
            if($db->getNumRows() > 0) {
                return true;
            } else {
                return false;
            }
        }
    }
    
    public static function getProjects($forSelect = false) {
        $db = Database::getInstance();
        if($forSelect) {
            $db->query("SELECT pid as `id`, name as `text` FROM projects");
            $groups = array();
            $groups[] = array('id'=>0,'text'=>'--- Wybierz projekt ---');
            while($group = $db->getRowAssoc()) {
                $groups[] = $group;
            }
            return $groups;
        } else {
            $db->query("SELECT * FROM projects");
            if($db->getNumRows() > 0) {
                return $db->getObjectArray();
            } else {
                return array();
            }
        }
    }
    public static function removeWorks($projectOrTask) {
        $db = Database::getInstance();
        if(get_class($projectOrTask) == "Project") {
            $db->query("DELETE FROM works WHERE pid=" . $projectOrTask->getPid());
        } else {
            $db->query("DELETE FROM works WHERE ptid=" . $projectOrTask->getPtid());
        }
        return $db->getAffectedRows();
    }
    
    public static function removeSignedUsers($projectOrTask) {
        if(get_class($projectOrTask) == "Project") {
            $tasks = ProjectDAO::getTasks($projectOrTask);
            foreach($tasks as $key => $task) {
                ProjectDAO::removeSignedUsers($task);
            }
        } else {
            $db = Database::getInstance();
            $db->query("DELETE FROM project_tasks_to_users WHERE ptid=" . $projectOrTask->getPtid());
            return $db->getAffectedRows() > 0;
        }
    }
    public static function removeTasks($project) {
        $db = Database::getInstance();
        $db->query("DELETE FROM project_tasks WHERE pid=" . $project->getPid());
        return $db->getAffectedRows();
    }
    
    public static function removeProject($project) {
        $db = Database::getInstance();
        $db->query("DELETE FROM projects WHERE pid=" . $project->getPid());
        return $db->getAffectedRows() > 0;
    }
    public static function getTaskInfo($ptid) {
        $db = Database::getInstance();
        $db->query("SELECT * FROM project_tasks WHERE ptid=" . $ptid);
        if($db->getNumRows() > 0) {
            $data = $db->getRowAssoc();
            $db->query("SELECT uid FROM project_tasks_to_users WHERE ptid=" . $ptid);
            if($db->getNumRows() > 0) {
                $data = array_merge($data,$db->getRowAssoc());
            }
            return $db->dbToObjectKeys($data);
        } else {
            return array();
        }
    }
    public static function taskExists($project, $taskName) {
        $db = Database::getInstance();
        $db->query("SELECT * FROM project_tasks WHERE pid=" . $project->getPid() . " AND name LIKE '$taskName'");
        return $db->getNumRows() > 0;
    }
    public static function createTask($task) {
        $db = Database::getInstance();
        $db->query("INSERT INTO project_tasks(pid,name,description,priority,date_start,date_end) VALUES(
             " . $task->getPid() . ",
            '" . $task->name . "',
            '" . $task->description . "',
             " . $task->priority . ",
            '" . $task->dateStart . "',
            '" . $task->dateEnd . "'
            )");
        $ptid = $db->insertId(); 
        if($task->getUser() > 0) {
            $db->query("INSERT INTO project_tasks_to_users VALUES(" . $ptid . ',' . $task->getUser() . ")");
        }
        return $ptid;
    }
    public static function updateTask($task) {
        $db = Database::getInstance();
        $db->query("UPDATE
                        project_tasks
                    SET
                        name = '" . $task->name . "',
                        description = '" . $task->description . "',
                        date_start = '" . $task->dateStart . "',
                        date_end = '" . $task->dateEnd . "',
                        priority = " . $task->priority . ",
                        date_done = '" . $task->getDateDone() . "',
                        done = " . $task->getDone() . "
                    WHERE
                        ptid = " . $task->getPtid());
        $rows = $db->getAffectedRows();
        if($task->getUser() > 0) {
            $db->query('SELECT * FROM project_tasks_to_users WHERE ptid=' . $task->getPtid());
            if($db->getNumRows() > 0) {
                $db->query("UPDATE project_tasks_to_users SET uid=" . $task->getUser() . " WHERE ptid=" . $task->getPtid());
                $rows += $db->getAffectedRows();
            } else {
                $db->query("INSERT INTO project_tasks_to_users(uid, ptid) VALUES(" . $task->getUser() . "," . $task->getPtid() . ')');
                $rows += $db->getAffectedRows();
            }
        }
        return $rows > 0;
    }
    public static function taskAtProgress($task) {
        $db = Database::getInstance();
        $db->query("SELECT * FROM works WHERE ptid=" . $task->getPtid() . " AND ended=0");
        return $db->getNumRows() > 0;
    }
    public static function removeTask($task) {
        $db = Database::getInstance();
        $db->query("DELETE FROM project_tasks WHERE ptid=" . $task->getPtid());
        return $db->getAffectedRows() > 0;
    }
}

?>